package com.egobuy.search.controller;

import com.egobuy.entity.Item;
import com.egobuy.pojo.BuyResult;
import com.egobuy.pojo.EgoBuyPager;
import com.egobuy.search.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/search")
@CrossOrigin(allowedHeaders = "*",value = "*",origins = "*",methods = {RequestMethod.GET,RequestMethod.POST},maxAge = 3600)
public class SearchController {
    @Autowired
    private SearchService searchService;
    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping(value = "/search",produces = "application/json;charset=utf-8")
    public BuyResult<EgoBuyPager<Item>> search(String keyword, @RequestParam(defaultValue = "1")int page, @RequestParam(defaultValue = "60") int size){
        return searchService.search(keyword,page,size);
    }
    //商品添加 远程访问 save 添加索引
    @RequestMapping(value="/save",produces = "application/json;charset=utf-8")
    public BuyResult save(long itemId){
        // 通过远程调用查询item对象
        Item item = restTemplate.getForObject("http://localhost:11011/product-api/item/queryId?itemId=" + itemId, Item.class);
        return searchService.save(item);
    }

}